import { Vector3 } from '../Math/Vector3'
import { getStream } from '../Core/connect'
import * as MathUtils from '../Math/MathUtils'

export class Camera {
    readonly type: string
    uuid: string
    position: Vector3
    constructor() {
        this.type = 'Camera'
        this.uuid = MathUtils.generateUUID()
        this.position = new Vector3(0, 0, 0)
    }
    getCameraMatrix() {
        const uuid = this.uuid
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { cameraFunction: 'getCameraMatrix', sessionId: sessionId, ids: `${uuid}` }
            stream.emitUIInteraction(message)
            const getMatrixReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) { 
                    resolve(returnData)
                    stream.removeResponseEventListener("getCameraMatrix")
                }
            }
            stream.addResponseEventListener("getCameraMatrix", getMatrixReady)
        })
    }
    // canInterrupt:飞行时能否被打断
    flyToLonLat(position: Vector3, pitch: number, yaw: number,canInterrupt:boolean = false) {
        this.position = position
        const stream = getStream()
        const str = {cameraFunction: 'flyToLonLat', position: `${this.position.x},${this.position.y},${this.position.z}`, pitch: `${pitch}`, yaw: `${yaw}`,canInterrupt:canInterrupt}
        stream.emitUIInteraction(str)
    }

    flyToWorld(position: Vector3, pitch: number, yaw: number,canInterrupt:boolean = false) {
        const stream = getStream()
        const str = {cameraFunction: 'flyToWorld', position: `${position.x},${position.y},${position.z}`, pitch: `${pitch}`, yaw: `${yaw}`,canInterrupt:canInterrupt}
        stream.emitUIInteraction(str)
    }
    // 锁定相机，不可移动
    lock() {
        const stream = getStream()
        const str = {cameraFunction: 'lockCamera'}
        stream.emitUIInteraction(str)
    }
    unLock() {
        const stream = getStream()
        const str = {cameraFunction: 'unLockCamera'}
        stream.emitUIInteraction(str)
    }
    setCameraMatrix(position: Vector3, rotate: Vector3){
        const stream = getStream()
        const str = {cameraFunction: 'setCameraMatrix', position: position, rotate: rotate}
        stream.emitUIInteraction(str)
    }
    setCameraMatrixLonLat(position: Vector3, rotate: Vector3){
        const stream = getStream()
        const str = {cameraFunction: 'setCameraMatrixLonLat', position: position, rotate: rotate}
        stream.emitUIInteraction(str)
    }
}